/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.classdiagram.editpart;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.uml2.uml.Component;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.ElementImport;
import org.eclipse.uml2.uml.NamedElement;

import com.cea.papyrus.core.PapyrusPlugin;
import com.cea.papyrus.core.directedit.IDirectEdition;
import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.core.figures.IconFigure;
import com.cea.papyrus.core.figures.NodeFigure;
import com.cea.papyrus.core.helper.ModelerHelper;
import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.diagram.classdiagram.figures.ComponentFigure;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;
import com.cea.papyrus.diagraminterchange2.di2.Uml1SemanticModelBridge;

/**
 * Controller class for {@link Component}.
 */
public class ComponentEditPart extends ClassEditPart {
	
	/** Debug channel for this edit part. */
	protected final int channel = 202;
	
//	/**
//	 * Manager for editing Component name.
//	 */
//	private DirectEditManager manager;

	/**
	 * Creates a new ComponentEditPart.
	 * @param graphNode the model graph node for this edit part
	 * @param modelManager the model manager associated to the edit part
	 */
	public ComponentEditPart(GraphNode graphNode, ModelManager modelManager) {
		super(graphNode, modelManager);
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	protected IFigure createFigure() {
		// Force initialization of the manager when figure has changed
		manager = null;	
		directEditionMode = IDirectEdition.UNDEFINED_DIRECT_EDITOR;
		// Try to get figure from ContainerWithSemanticEditPart
		/** Stereotype presentation */
		IFigure fig=null;
		String stereotypePresentation = getPresentation();
		if(ModelerHelper.IMAGE_STEREOTYPE_PRESENTATION.equals(stereotypePresentation)) {
			org.eclipse.swt.graphics.Image img = PapyrusPlugin.getShapeElement(getUmlElement());
			if (img != null) {			
				fig= new IconFigure(img, getGraphNode().hashCode());
			}
		}	
		if (fig != null) {
			return fig;
		}
		// else
		return new ComponentFigure();				
	}
	
	/**
	 * Returns the Name Label of this edit part.
	 * @return Label the label figure from the graphical representation
	 */
	protected Label getNameLabel() {
		IFigure fig = getFigure();
		if (fig instanceof IconFigure) {
			return (Label) fig;
		} else if (fig instanceof ComponentFigure) {
			return ((ComponentFigure)fig).getNameLabel();
		} else {
			Debug.error(this, "ComponentEditPart::getNameLabel() : Unknown Figure type.", channel);
			return null;
		}
	}
	
//	/**
//	 * {@inheritDoc}
//	 */
//	@Override
//	protected void createEditPolicies() {
//		super.createEditPolicies();
//	}

	/** 
	 * @{inheritDoc} 
	 */
	@Override
	public org.eclipse.uml2.uml.Component getUmlElement() {
		Element e=((Uml1SemanticModelBridge)getGraphNode().getSemanticModel()).getElement();
		// case of element import
		if(e instanceof ElementImport){
			return (org.eclipse.uml2.uml.Component)((ElementImport) e).getImportedElement();
		}
		// this is a simple Component
		return (org.eclipse.uml2.uml.Component)e;
	}

	/** 
	 * @{inheritDoc} 
	 */
	@Override
	protected void refreshVisuals() {
		Debug.debug(this, "refreshvisuals", channel);

		Dimension size = getGraphNode().getSize();
		Point p = getGraphNode().getPosition();
		Rectangle r = new Rectangle(p ,size);

		IFigure fig = getFigure();

		// if fig is a stereotype figure
		if (!(fig instanceof ComponentFigure)) {
			IconFigure icf= (IconFigure)getFigure();
			icf.setName(((NamedElement)getUmlElement()).getName());
		} else {
			// Retrieve current component and figure
			org.eclipse.uml2.uml.Component currentComponent = getUmlElement();
			com.cea.papyrus.umlutils.Element currentElement = new com.cea.papyrus.umlutils.Element(getUmlElement());

			ComponentFigure f = (ComponentFigure) fig; //get Component figure

			// if fig is a ClassFigure
			((NodeFigure) fig).refreshColors(getGraphNode());
			((NodeFigure) fig).refreshPresentation(getPresentation(), currentComponent, getGraphNode());
			boolean hasIcon  = currentElement.hasIcon();
			boolean hasShape = currentElement.hasShape();

			//f.refreshStereotypes(currentElement.getStereotypes(), getPresentation(), hasIcon, hasShape);
			f.refreshStereotypes(f.stereotypesLabel(this), getPresentation(), hasIcon, hasShape);

			f.setName(currentComponent.getName());// set the name of the class

			// refresh qualifiedName
			GraphNode gp= (GraphNode)getGraphNode().getContainer();
			f.refreshQualifiedName(gp, getGraphNode(), getUmlElement());
		}
		((GraphicalEditPart) getParent()).setLayoutConstraint(this, fig, r);
	}

//	/**
//	 * Returns the Figure of this as a PackageFigure.
//	 * @return PackageFigure of this.
//	 */
//	protected ClassifierFigure getPackageBoardFigure() {
//		return (ClassifierFigure)getFigure();
//	}

}